import {
  getTaskTable
  // editTaskDataRecord
} from '../../api/task'
import {
  TASK_DATA,
  TASK_DATA_CURRENT_ROW,
  TASK_DATA_DATA_COUNT,
  TASK_DATA_HISTORY_DATA,
  TASK_DATA_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    taskData: [],
    taskDataDataCount: null,
    taskDataPageSize: 20,
    taskDataHistoryData: [],
    row: {
      ctime: '',
      time: '',
      type: '',
      method: '',
      task: '',
      title: '',
      taskmoney: '',
      receive: '',
      mouth: '',
      contract: '',
      sum: ''
    }
  },
  getters: {
    taskData: state => state.taskData
  },
  mutations: {
    [TASK_DATA] (state, data) {
      state.taskData = data
    },
    [TASK_DATA_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [TASK_DATA_DATA_COUNT] (state, data) {
      state.taskDataDataCount = data
    },
    [TASK_DATA_HISTORY_DATA] (state, data) {
      state.taskDataHistoryData = data
    },
    [TASK_DATA_PAGE_SIZE] (state, data) {
      state.taskDataPageSize = data
    }
  },
  actions: {
    async getTaskData ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getTaskTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          ctime: item.ctime,
          btime: item.btime,
          etime: item.etime,
          // type: '活动推广',
          type: item.type.toString(),
          name: item.name,
          cname: item.cname,
          budget: item.budget,
          perrate: item.perrate,
          permoney: item.permoney,
          receive: item.receive,
          mouth: item.mouth,
          num: item.num,
          readcnt: item.readcnt ? item.readcnt : 0,
          price: item.price
          // time: item.time,
          // type: item.type,
          // method: item.method,
          // task: item.task,
          // title: item.title,
          // taskmoney: item.taskmoney,
          // receive: item.receive,
          // mouth: item.mouth,
          // contract: item.contract,
          // sum: item.sum
          // time: item.time,
          // type: item.type,
          // method: item.method,
          // task: item.task,
          // taskRequire: item.taskRequire,
          // budget: item.budget,
          // traffic: item.traffic,
          // contentType: item.contentType,
          // sticky: item.sticky,
          // open: item.open,
          // missionMoney: item.missionMoney,
          // price: item.price,
          // commision: item.commision,
          // title: item.title,
          // taskmoney: item.taskmoney,
          // order: item.order,
          // state: item.state
        }]
      })
      commit(TASK_DATA, newData)
      commit(TASK_DATA_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.taskData.taskDataPageSize) {
        commit(TASK_DATA_HISTORY_DATA, newData)
      } else {
        commit(TASK_DATA_HISTORY_DATA, newData.slice(0, rootState.taskData.taskDataPageSize))
      }
      return resp
    },
    // async editTaskData ({ commit, rootState }, playload) {
    //   let data = {}
    //   if (Object.keys(playload).length > 0) {
    //     data = playload
    //   }
    //   await editTaskDataRecord(data)
    // },
    async getTaskDataCurrentRow ({ commit, rootState }, row) {
      commit(TASK_DATA_CURRENT_ROW, row)
    },
    async changeTaskDataPage ({ commit, rootState }, index) {
      let data = rootState.taskData.taskData
      let _start = (index - 1) * rootState.taskData.taskDataPageSize
      let _end = index * rootState.taskData.taskDataPageSize
      commit(TASK_DATA_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeTaskDataPageSize ({ commit, rootState }, value) {
      commit(TASK_DATA_PAGE_SIZE, value)
    }
  }
}
